import _ from 'lodash'
import Vue from 'vue'
import { SEARCH, PAGE, DIC, INFO, SAVE, APPD } from '@/store/modules/crud/keys'
import { CrudStates, CrudSearch, CrudPage, CrudDic, CrudInfo, CrudAppd } from '@/store/modules/crud/typings'

export default {
  [SEARCH] (state: CrudStates, { name, key, value, params } : { name: string, key?: string, value?: any, params: CrudSearch }) {
    if (!state[name]) return
    if (key) {
      Vue.set(state[name].search, key, value)
    } else {
      state[name].search = params ? _.merge(state[name].search, params) : state[name].defaultSearch
    }
  },

  [PAGE] (state: CrudStates, { name, page }: { name: string, page: CrudPage }) {
    if (!state[name]) return
    state[name].page = page
  },

  [DIC] (state: CrudStates, { name, dic }: { name: string, dic: CrudDic }) {
    if (!state[name]) return
    state[name].dic = dic
  },

  [SAVE] (state: CrudStates, { name, info }: { name: string, info: any }) {
    if (!state[name]) return
    state[name].info = info
  },

  [INFO] (state: CrudStates, { name, info, key, value }: { name: string, info?: CrudInfo, key?: string, value?: any }) {
    if (!state[name]) return
    if (key) {
      Vue.set(state[name].info, key, value)
      state[name].isModify = true
    } else {
      state[name].info = info || {}
      state[name].isModify = false
    }
  },
  //应用页面公共数据 application page public data
  [APPD] (state: CrudStates, { name, appd, key, value }: { name: string, appd?: CrudAppd, key?: string, value?: any }) {
    if (!state[name]) return
    if (key) {
      Vue.set(state[name].appd, key, value)
      state[name].isModify = true
    } else {
      state[name].appd = appd || {}
      state[name].isModify = false
    }
  },
}
